package com.linln.admin.system.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.linln.admin.system.validator.DictValid;
import com.linln.common.enums.ResultEnum;
import com.linln.common.enums.StatusEnum;
import com.linln.common.exception.ResultException;
import com.linln.common.utils.EntityBeanUtil;
import com.linln.common.utils.HttpServletUtil;
import com.linln.common.utils.ResultVoUtil;
import com.linln.common.utils.StatusUtil;
import com.linln.common.vo.ResultVo;
import com.linln.component.thymeleaf.utility.DictUtil;
import com.linln.modules.system.domain.Dict;
import com.linln.modules.system.service.DictService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 小懒虫
 * @date 2018/8/14
 */
@Controller
@RequestMapping(value = "/system/dict",name = "字典表管理")
public class DictController {

    @Autowired
    private DictService dictService;

    /**
     * 列表页面
     */
    @GetMapping(value = "/index",name = "列表页")
    @RequiresPermissions("system:dict:index")
    public String index(Model model, Dict dict){

        int pageNum = HttpServletUtil.getParameterInt("page", PageSort.PAGE_NUMBER_DEF);
        int pageSize = HttpServletUtil.getParameterInt("size", PageSort.PAGE_SIZE_DEF);
        PageInfo<Dict> pageInfo = dictService.findByPage(pageNum,pageSize,dict);

        // 封装数据
        model.addAttribute("list", null !=pageInfo && null !=pageInfo.getList() ? pageInfo.getList() : null);
        model.addAttribute("page",pageInfo);
        return "/system/dict/index";
    }

    /**
     * 跳转到添加页面
     */
    @GetMapping(value = "/add",name = "添加页面")
    @RequiresPermissions("system:dict:add")
    public String toAdd(){
        return "/system/dict/add";
    }

    /**
     * 跳转到编辑页面
     */
    @GetMapping(value = "/edit/{id}",name = "编辑页面")
    @RequiresPermissions("system:dict:edit")
    public String toEdit(@PathVariable("id") Long id, Model model){
        Dict dict = dictService.getByPK(id);
        model.addAttribute("dict", dict);
        return "/system/dict/add";
    }

    /**
     * 保存添加/修改的数据
     * @param valid 验证对象
     */
    @PostMapping(value = {"/add","/edit"},name = "保存或修改数据")
    @RequiresPermissions({"system:dict:add","system:dict:edit"})
    @ResponseBody
//    @ActionLog(name = "字典管理", message = "字典：${title}", action = SaveAction.class)
    public ResultVo save(@Validated DictValid valid, Dict dict){
        // 清除字典值两边空格
        dict.setValue(dict.getValue().trim());

        // 判断字典标识是否重复
        if (dictService.repeatByName(dict)) {
            throw new ResultException(ResultEnum.DICT_EXIST);
        }

        // 复制保留无需修改的数据
        if(dict.getId() != null){
            Dict beDict = dictService.getByPK(dict.getId());
            EntityBeanUtil.copyProperties(beDict, dict);
        }

        // 保存数据
        dictService.save(dict);
        if(dict.getId() != null){
            DictUtil.clearCache(dict.getName());
        }
        return ResultVoUtil.SAVE_SUCCESS;
    }

    /**
     * 跳转到详细页面
     */
    @GetMapping(value = "/detail/{id}",name = "详细页面")
    @RequiresPermissions("system:dict:detail")
    public String toDetail(@PathVariable("id") Long id, Model model){
        Dict dict = dictService.getByPK(id);
        model.addAttribute("dict",dict);
        return "/system/dict/detail";
    }

    /**
     * 设置一条或者多条数据的状态
     */
    @RequestMapping(value = "/status/{param}",name = "批量改状态")
    @RequiresPermissions("system:dict:status")
    @ResponseBody
//    @ActionLog(name = "字典状态", action = StatusAction.class)
    public ResultVo status(
            @PathVariable("param") String param,
            @RequestParam(value = "ids", required = false) List<Long> ids){
        // 更新状态
        StatusEnum statusEnum = StatusUtil.getStatusEnum(param);
        if (dictService.updateStatus(statusEnum, ids)) {
            return ResultVoUtil.success(statusEnum.getMessage() + "成功");
        } else {
            return ResultVoUtil.error(statusEnum.getMessage() + "失败，请重新操作");
        }
    }
}
